package com.cheng.supre.customcalendarview;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private CalendarView calendarView;
    private RecyclerView recyclerView;
    private ImageView imageView;
    private Button button;
    private int item_width;
    private List<String> txt = new ArrayList<>();
    private ItemAdapter itemAdapter;
    private int xxx = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        item_width = getWindowManager().getDefaultDisplay().getWidth()/ 2;
        calendarView = findViewById(R.id.calendar);
        calendarView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
        recyclerView = findViewById(R.id.recyclerview);
        imageView = findViewById(R.id.image);
        button =  findViewById(R.id.btn_camera);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this,CameraActivity.class));
            }
        });


        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
            @Override
            public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
                Toast.makeText(MainActivity.this, ""+year+"-"+(month+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();
            }
        });


        calendarView.setMaxDate(calendarView.getDate());

        imageView.setLayoutParams(new RelativeLayout.LayoutParams(item_width, RelativeLayout.LayoutParams.MATCH_PARENT));

        itemAdapter = new ItemAdapter(this, item_width, getTxt());
        LinearLayoutManager manager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
        recyclerView.setLayoutManager(manager);
        recyclerView.setAdapter(itemAdapter);
        itemAdapter.notifyDataSetChanged();


        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                xxx += dx;
                if (xxx >= item_width/4){
                    calendarView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                }else {
                    calendarView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                }
            }
        });
    }

    private List<String> getTxt() {
        for (int i = 0; i < 10; i++){
            txt.add("hahahah"+(i+1));
        }
        return txt;
    }
}
